const express = require('express')
const path = require("path")
const noteModel = require('../db/models/noteModel');

const loginVerify = require('../middleware/loginVerify');

const router = express.Router();

//新增笔记请求接口
router.post('/add',loginVerify,(req,res)=>{
    // 新增数据 - 数据库
    let {title,content} = req.body;
    let username = req.session.username;
    console.log(title,content)
    noteModel.create({
        title,
        content,
        username
    },(err,data)=>{
        if(err) throw err;
    })
})
//列表
router.post("/list", (req, res) => {
    noteModel.find((err, data) => {
      res.send(data)
    })
  })
  
// 获取编辑页面数据
router.post("/editData", loginVerify,(req, res) => {

    let {id} = req.body
  
    noteModel.findById(id, function (err, data) {
      res.send(data)
    });
  })
//编辑笔记请求接口
router.post("/edit", loginVerify,(req, res) => {
  let {id, titleV:title, contentV:content} = req.body
  console.log(id,title, content)
  noteModel.updateOne({_id:id},{title,content},function(err, data) {
    if(err) throw err
    console.log(data)
  })
})

//删除笔记请求接口
router.get('/delete',loginVerify,(req,res)=>{
    // 去数据库中删除一条数据
    let {id} = req.query
    console.log(id)
    noteModel.deleteOne({_id:id}, function(err, data) {
      if(err) throw err
      console.log(data)
    })
    res.send("删除成功")
  })
  

module.exports = router;